package github.leavesczy.reactivehttpsamples.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import github.leavesczy.reactivehttpsamples.R;
import github.leavesczy.reactivehttpsamples.util.exception.BusException;

public class BridgeSpanItem extends LinearLayout {

    private List<EditText> childViews = new ArrayList<>();

    private LinearLayout llContainer;

    private Context mContext;

    private int rightTextColor;
    private float rightTextSize;
    private int inputType;

    private TextView buttonTextView;
    private OnClickListener btnClickListener;
    public BridgeSpanItem(Context context) {
        this(context, null);
    }

    public BridgeSpanItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BridgeSpanItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        try (TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BridgeSpanItem)) {
            String leftText = ta.getString(R.styleable.BridgeSpanItem_bSpanLeftText);
            int leftTextColor = ta.getColor(R.styleable.BridgeSpanItem_bSpanLeftTextColor, Color.BLACK);
            float leftTextSize = ta.getDimension(R.styleable.BridgeSpanItem_bSpanLeftTextSize, 14);

            String leftBtnText=ta.getString(R.styleable.BridgeSpanItem_bSpanButtonText);
            int leftBtnTextColor = ta.getColor(R.styleable.BridgeSpanItem_bSpanButtonTextColor, Color.BLACK);
            float leftBtnTextSize = ta.getDimension(R.styleable.BridgeSpanItem_bSpanButtonTextSize, 12);

            rightTextColor = ta.getColor(R.styleable.BridgeSpanItem_bSpanRightTextColor, Color.BLACK);
            rightTextSize = ta.getDimension(R.styleable.BridgeSpanItem_bSpanRightTextSize, 14);
            inputType = ta.getInt(R.styleable.BridgeSpanItem_bSpanRightInputType, 0);


            setOrientation(LinearLayout.HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);

            setWeightSum(5);

            //添加左侧 Text 容器
            LinearLayout leftContain=new LinearLayout(context);
            leftContain.setOrientation(LinearLayout.HORIZONTAL);
            LayoutParams leftContainParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            leftContainParams.weight = 2;
            leftContainParams.gravity=Gravity.CENTER_VERTICAL;
            leftContainParams.leftMargin = SizeUtils.dp2px(10);
            //text
            TextView leftTextView = new TextView(context);
            leftTextView.setText(leftText);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);
            LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            leftParams.rightMargin = SizeUtils.dp2px(5);
            leftParams.gravity=Gravity.CENTER_VERTICAL;
            leftTextView.setGravity(Gravity.CENTER_VERTICAL);
            leftContain.addView(leftTextView,leftParams);
            //判断是否展示按钮
            if(!StringUtils.isEmpty(leftBtnText)){
                buttonTextView=new TextView(context);
                buttonTextView.setTextColor(leftBtnTextColor);
                buttonTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftBtnTextSize);
                buttonTextView.setText(leftBtnText);
                buttonTextView.setPadding(SizeUtils.dp2px(5),SizeUtils.dp2px(1),SizeUtils.dp2px(5),SizeUtils.dp2px(1));
                buttonTextView.setBackgroundResource(R.drawable.shape_edit_button_btn);
                LinearLayout.LayoutParams rightBtParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                rightBtParams.gravity=Gravity.CENTER_VERTICAL;
                leftContain.addView(buttonTextView,rightBtParams);
                buttonTextView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(btnClickListener!=null){
                            btnClickListener.onClick(v);
                        }
                    }
                });
            }
            addView(leftContain, leftContainParams);

            //添加右侧的 HorizontalScrollView 滚动容器
            HorizontalScrollView rightBg = new HorizontalScrollView(context);
            LayoutParams rightParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            rightParams.weight = 3;
            rightParams.rightMargin = SizeUtils.dp2px(10);
            //添加容器view
            addView(rightBg, rightParams);

            //横向存放的容器
            llContainer = new LinearLayout(context);
            llContainer.setOrientation(LinearLayout.HORIZONTAL);
            llContainer.setGravity(Gravity.CENTER_VERTICAL);
            rightBg.addView(llContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


            setBackgroundResource(R.drawable.shape_bridge_edit_item_bg);

        }
    }


    private void addEditTextView(int index,String content) {
        //添加右侧的 EditText
        EditText editTextView = new EditText(mContext);
        editTextView.setTextColor(rightTextColor);
        editTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);
        setInputType(editTextView, inputType);
        editTextView.setLines(1);
        LinearLayout.LayoutParams rightParams = new LayoutParams(SizeUtils.dp2px(40), LayoutParams.MATCH_PARENT);
        rightParams.rightMargin = SizeUtils.dp2px(5);
        editTextView.setGravity(Gravity.CENTER_VERTICAL);
        if (!StringUtils.isEmpty(content)) {
            editTextView.setText(content);
        }else {
            editTextView.setHint((index+1)+"跨");
        }
        llContainer.addView(editTextView, rightParams);


        childViews.add(editTextView);

    }

    private void setInputType(EditText editText, int inputType) {
        if (inputType == 0) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
        } else if (inputType == 1) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else if (inputType == 2) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
    }

    public void initViewSize(int size) {
       initViewSize(size,null);
    }

    public void initViewSize(int size,String defaultValue) {
        llContainer.removeAllViews();
        childViews.clear();
        for (int i = 0; i < size; i++) {
            addEditTextView(i,defaultValue);
        }
    }


    public void setSpanData(List<String> lists) {
        llContainer.removeAllViews();
        childViews.clear();
        for (int i = 0; i < lists.size(); i++) {
            addEditTextView(i,lists.get(i));
        }
    }

    public void setSpanDataListStr(String listStr) {
        if (StringUtils.isEmpty(listStr)) {
            return;
        }
        List<String> parseArray = JSON.parseArray(listStr, String.class);
        setSpanData(parseArray);
    }

    public List<String> getSpanData() {
        List<String> spanData = new ArrayList<>();
        if (childViews.isEmpty()) {
            return spanData;
        }
        for (EditText view : childViews) {
            String content = view.getText().toString();
            if (!StringUtils.isEmpty(content)) {
                spanData.add(content);
            }
        }
        return spanData;
    }

    public void setSpanEditable(boolean editable) {
        if (childViews.isEmpty()) {
            return;
        }
        for (EditText view : childViews) {
            view.setEnabled(editable);
        }
        if(buttonTextView!=null){
            buttonTextView.setEnabled(editable);
        }
    }

    public Integer childSize(){
        return childViews.size();
    }

    public void initButtonTextListener(View.OnClickListener l){
       this.btnClickListener=l;
    }

    public void resetValue(List<String> newValues) throws Exception {
        if(newValues.size()!=childViews.size()){
            throw new BusException("数据大小不对等");
        }
        for (int i = 0; i < childViews.size(); i++) {
            childViews.get(i).setText(newValues.get(i));
        }
    }
}
